package com.chg.mymvc.servletoffront;

import com.chg.mymvc.entity.Item;
import com.chg.mymvc.entity.News;
import com.chg.mymvc.service.ItemServiceImpl;
import com.chg.mymvc.service.NewsServiceImpl;
import com.chg.mymvc.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/front.html")
public class FrontDataServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获得参数栏目点击的参数
        String perm="";
        if(req.getParameter("perm")==null){
            perm="娱乐";
        }else{
            perm=req.getParameter("perm");
        }



    //查出栏目表的数据
        ItemServiceImpl is= new ItemServiceImpl();
        List<Item> itemlist = is.queryAll(1, 5);
               //查出新闻表的数据
        NewsServiceImpl ns= new NewsServiceImpl();


        //分页
        //获取用户提交的当前页码
        int pagesize=5;
        String num=req.getParameter("pagenum");
        int pagenum=num==null? 1:Integer.parseInt(num);


//创建分页工具
        Page page = new Page();
        page.setPageNum(pagenum);
        page.setPageSize(pagesize);
//获取总条数
        page.setTotal(is.allcount());

//传list为了展示
        System.out.println(perm+"我是perm");
        List<News> list = ns.queryAllofperm(page.getPageNum(), page.getPageSize(),perm);
        System.out.println(list+"我是新闻列表");


        req.setAttribute("newslist",list);
        req.setAttribute("itemlist",itemlist);

        req.getRequestDispatcher("page2/main.jsp").forward(req,resp);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
